/* ---------------------------------------------------------------------------------------------
 *
 *            Capital Alliance Software Confidential Proprietary
 *            (c) Copyright CAS 201{x}, All Rights Reserved
 *                          www.pekall.com
 *
 * ----------------------------------------------------------------------------------------------
 */

package com.pekall.myDictionary.utils;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

/**
 * @author leikang
 */
public class SQLiteNewWords {
    private final String TAG = SQLiteNewWords.class.getCanonicalName();

    private static final String DataBaseName = "MyDictionary";

    SQLiteDatabase db;

    Context context;

    public SQLiteNewWords(Context context) {
        this.open(context);
    }

    public void deleteTabel() {
        String sql = " drop table T_NEW_WORDS";
        this.db.execSQL(sql);
    }

    private void createTabel() {
        String sql = "";
        try {
            sql = "CREATE TABLE IF NOT EXISTS T_NEW_WORDS    (_ID INTEGER PRIMARY KEY autoincrement, _WORD TEXT)";
            this.db.execSQL(sql);
        } catch (Exception e) {
            Log.d(TAG, "[Create tabel]" + e);
        } finally {
        }
    }

    public boolean save(String word) {
        String sql = "insert into T_NEW_WORDS values(null,'" + word + "')";
        try {
            this.db.execSQL(sql);
            return true;
        } catch (Exception e) {
            return false;
        } finally {
        }
    }

    public Cursor loadAll() {
        Cursor cur = db.query("T_NEW_WORDS", new String[] {
                "_ID", "_WORD"
        }, null, null, null, null, "_WORD");
        return cur;
    }

    public void open(Context context) {
        if (null == db || !this.db.isOpen()) {
            this.context = context;
            this.db = context.openOrCreateDatabase(DataBaseName, Context.MODE_PRIVATE, null);
            createTabel();
        }
    }

    public void close() {
        db.close();
    }

}
